//
//  XHShengHuoTongViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-26.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//
#define urlString @[@"http://www.syzwzx.com.cn/index/index!list.action",\
@"http://m.hao123.com/",\
@"http://m.sx988.com/00/dh.htm",\
@"http://59.49.18.116:8008/sxwwpt_wai/jsp/inquires/querymotorIllegalc.jsp",\
@"http://qq.ip138.com/daishoudian/shanx/TaiYuan.html",\
@"http://wap.8684.cn/bus.php?cityid=47",\
@"http://d.tiantiantiao.com/item.aspx",\
@"http://wap.yikuaiqu.com",\
@"http://t.qunar.com/index.jsp?bd_source=&v=2",\
@"http://m.kuaidi100.com",\
@"http://wap.ip138.com/post.html",\
@"http://m.sx988.com/00/jr.htm",]
#import "XHShengHuoTongViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AFNetworking.h"
#import "MainEnterViewController.h"
#import "dangeViewController.h"
#import "shenghuotongWebviewController.h"
@interface CustomButton : UIButton
@end

@implementation CustomButton

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        
    }
    return self;
}

-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGRect tempRect;
    tempRect = CGRectMake(22.5, 10, 45, 45);
    return tempRect;
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGRect tempRect;
    tempRect = CGRectMake(0, self.frame.size.height-18, self.frame.size.width, 12);
    return tempRect;
}

@end

@interface XHShengHuoTongViewController ()
-(IBAction)hitButton:(id)sender;
@property (nonatomic,strong) UIView *buttonView;
@end

@implementation XHShengHuoTongViewController
@synthesize buttonView = _buttonView;
@synthesize dataArr;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.frame = [[UIScreen mainScreen] bounds];
        CGRect rect = self.view.frame;
        CGFloat oY = kIOS_VERSION<7.0?44:64;
        
        self.titleLable.text = @"生活通";
        @weakify(self);
        [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [self.leftButton setImage:[UIImage imageNamed:@"icon_back@2x"] forState:UIControlStateNormal];
        
        self.rightButton.hidden = YES;
        [self.view bringSubviewToFront:self.navBar];
        dataArr = [NSArray array];
    
        
    }
    return self;
}

-(void)buttton:(UIButton*)button
{
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGFloat oY = kIOS_VERSION<7.0?44:64;
    NSArray* array = @[@"便民信息",@"办事指南",@"常用电话",@"违章查询",@"火车票代售点",@"公交查询",@"菜价查询",@"景点门票",@"列车查询",@"快递查询",@"邮编查询",@"节日大全"];
    _buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navBar.frame.size.height+oY, 320, self.view.frame.size.height-self.navBar.frame.size.height)];
    
    _buttonView.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < [array count]; i++ )
    {
        CustomButton* button = [[CustomButton alloc] initWithFrame:CGRectMake(90*(i%3)+(i%3)*10+10, 80*(i/3)+(i/3)*10+10, 90,80)];
        [button setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
        UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"cop_%d.png",i]];
        [button setImage:image forState:UIControlStateNormal];
        button.tag = 10000+i;
        if (i%3 == 0)
        {
            [button setBackgroundColor:[UIColor colorWithRed:26.0/255 green:166.0/255 blue:253.0/255 alpha:1.0]];
        }
        else if (i%3 == 1)
        {
            [button setBackgroundColor:[UIColor colorWithRed:173.0/255 green:214.0/255 blue:84.0/255 alpha:1.0]];
            
        }
        else
        {
            [button setBackgroundColor:[UIColor colorWithRed:255.0/255 green:128.0/255 blue:25.0/255 alpha:1.0]];
        }
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_buttonView addSubview:button];
    }
    [self.view addSubview:_buttonView];
    [self loaddata];

}
-(void)buttonAction:(CustomButton*)button
{
    NSString* str = [urlString objectAtIndex:button.tag-10000];
    if ([str length] <= 0)
        return;
    if (button.tag == 10000)
    {
        if ([dataArr count] == 0)
        {
            return;
        }
        MainEnterViewController* v2 = [[MainEnterViewController alloc] init];
        v2.yinitData = @{@"childList":dataArr,@"name":@"便民信息"};
        v2.damokuai = @"便民信息";
        [self.navigationController pushViewController:v2 animated:YES];
        
    }
    else if (button.tag == 10001)
    {
        dangeViewController* viewContr = [[dangeViewController alloc] init];
        viewContr.newdId = @"56";
        NSLog(@"id = %@",viewContr.newdId);
        viewContr.titleString = @"办事指南";
        [self.navigationController pushViewController:viewContr animated:YES];
    }
    else
    {
        NSURL* url = [NSURL URLWithString:str];
        shenghuotongWebviewController* viewController = [[shenghuotongWebviewController alloc] init];
        viewController.url = url;
        viewController.webView.scalesPageToFit = NO;
        [self.navigationController pushViewController:viewController animated:YES];
    }
    
    
    
}
-(void)loaddata
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString* ST = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/categoryListByParent-55-3.html"];
    [manager GET:ST parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
           // self.titleArray = (NSArray *)responseObject;
            //NSLog(@"%@",(NSArray*)responseObject);
            dataArr = responseObject;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
      
        NSLog(@"2222%@",error);
    }];
    

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
